<?php

// 签到

namespace app\shop\controller;

use think\Db;
use think\Request;
use think\Session;
use think\Controller;
use app\shop\model\Config as ConfigModel;
use app\shop\model\Member as MemberModel;
use app\shop\model\MemberSign as MemberSignModel;
use app\shop\model\IntegralGoods as IntegralGoodsModel;

class Sign extends Controller
{
    /**
     * 签到页
     * @param Request $request
     * @param MemberModel $member
     * @param IntegralGoodsModel $goods
     * @return mixed
     */
    public function index(Request $request,MemberModel $member,IntegralGoodsModel $goods)
    {
        // 获取会员ID
        $userId = session('member_id');

        // 会员积分
        $integral = $member->where(['id' => $userId])->field('integral,id')->find();

        // 获取参数
        $inquire = $request->param();
        $sort = ['sort' => 'desc', 'id' => 'desc'];
        if (!empty($inquire['orderId'])) $sort = [$inquire['orderId'] => 'desc'];

        $integralGoods = $goods->order($sort)->paginate(20,false,['query' => $inquire]);

        // 判断是否签到
        $checkSign = self::checkSign($userId);

        return $this->fetch('', [
            'mate_head'      => '积分商城',
            'integral_goods' => $integralGoods,
            'integral'       => $integral,
            'check_sign'     => $checkSign
        ]);
    }


    /**
     * 会员签到
     * @return array
     */
    public function memberSign()
    {
        // 获取会员ID
        $memberId = session('member_id');

        // 每天签到获取的积分数
        // $web_sign = (int)ConfigModel::where('varname', 'web_sign')->value('varvalue');

        // 启动事务
        Db::startTrans();

        try {
            // 昨天是否签到 签到获得积分
            $ini = [
                'member_id' => Session::get('member_id'),
                'sign_time' =>  $a = date("Y-m-d",strtotime("-1 day")),
            ];
            $find = Db::name('member_sign')->where($ini)->find();
            if (empty($find)) {
                $sign_pre = 1;
                // 添加记录
                Db::name('member_sign')->insert(['member_id' => Session::get('member_id'), 'sign_time' => date('Y-m-d'),'sign'=>$sign_pre]);
                // 增加积分
                Db::name('member')->where(['id' => Session::get('member_id')])->setInc('integral', $sign_pre);
                //增加获取积分记录
                Db::name('integralRecord')->insert(['member_id' => Session::get('member_id'), 'type_id' => 1, 'type' => '签到', 'integral' => $sign_pre, 'title' => '签到', 'create_time' => time()]);
            }else{
                if ($find['sign'] == 64) {
                    $sign_pre = 1;
                    // 添加记录
                    Db::name('member_sign')->insert(['member_id' => Session::get('member_id'), 'sign_time' => date('Y-m-d'),'sign'=>$sign_pre]);
                    // 增加积分
                    Db::name('member')->where(['id' => Session::get('member_id')])->setInc('integral', $sign_pre);
                    //增加获取积分记录
                    Db::name('integralRecord')->insert(['member_id' => Session::get('member_id'), 'type_id' => 1, 'type' => '签到', 'integral' => $sign_pre, 'title' => '签到', 'create_time' => time()]);
                }else{
                    $sign_pre = $find['sign']*2;

                    // 添加记录
                    Db::name('member_sign')->insert(['member_id' => Session::get('member_id'), 'sign_time' => date('Y-m-d'),'sign'=>$sign_pre]);
                    // 增加积分
                    Db::name('member')->where(['id' => Session::get('member_id')])->setInc('integral', $sign_pre);
                    //增加获取积分记录
                    Db::name('integralRecord')->insert(['member_id' => Session::get('member_id'), 'type_id' => 1, 'type' => '签到', 'integral' => $sign_pre, 'title' => '签到', 'create_time' => time()]);
                }
            }
            // 提交事务
            Db::commit();
            return ['code' => 200, 'message' => '签到成功,请明天再来'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => 400, 'message' => '网络繁忙'];
        }
    }


    /**
     * 检查登录
     * @param $id
     * @return string
     */
    public function checkSign($id)
    {
        // 判断memberId 是否为空
        if (empty($id)) return "signNo";

        // 筛选条件
        $condtion = [
            'member_id' => $id,
            'sign_time' => date('Y-m-d', time()),
        ];

        // 判断是否签到
        $operation = (new MemberSignModel)->where($condtion)->find();
        if ($operation) {
            return "signIn";
        }
        return "signNo";
    }
}